/*==============================================================================

Description: Conduct bootstrap inference on bias-corrected AR(1) coefficient.

==============================================================================*/

clear
set more off
set scheme  s1mono 

*Begin output log file:
cap log close
log using "${logs}/prior_analysis_all_phases_nickell_inference.log", replace


set seed 1

mata mata clear

mata:
function nickellbc(x) {

return(-(1+x)/10*(1-1/10*(1-x^11)/(1-x))*((1-2*x/((1-x)*10))*(1-1/10*(1-x^10)/(1-x)))^(-1)+x - st_numscalar("rhohat"))
						}
end						

mata:
void nickell_solve() {

	mm_root(rhohat_bc=., &nickellbc(), -0.3, 0.2, 0, 1000)
	st_numscalar("est_bc", rhohat_bc)
											}
end

cap program drop nickell_inf
program nickell_inf, rclass
	
	count
	local bsize = r(N)/11

	preserve
	bsample `bsize', cluster(pnum) id(newpnum)
	
	xtset newpnum gorder
	
	xtreg y L.y, fe cluster(newpnum)
	scalar rhohat = _b[L.y] 
	
	mata: nickell_solve()
	
	restore
	
	return scalar est_bc = est_bc
	
end	
	
* Phase RDD1
use "${dat}/Pilot 1.0/survey_merged.dta", clear

destring playercorrect, replace
destring playerdiscontinuity_yn, replace

destring gorder,replace

keep if playertreatment_group == "A"

egen pnum = group(pid)

xtset pnum gorder


xtreg y L.y, fe cluster(pnum)
local orig_coeff = -0.0405133

simulate bs_est_bc=r(est_bc), rep(1000): nickell_inf

sort bs_est_bc
list if _n==975

sum bs_est_bc, d
gen dev2 = (bs_est_bc -`orig_coeff')^2 * 1000/999

sum dev2
di ""
di "bias corrected estimate is `orig_coeff'" 
di "standard error is " sqrt(r(mean))


* Phase RDD2
use "${dat}/Phase II/survey_merged_PhaseII.dta", clear

destring playercorrect, replace
destring playerdiscontinuity_yn, replace

destring gorder,replace

keep if playertreatment_group == "A"

egen pnum = group(pid)

xtset pnum gorder

xtreg y L.y, fe cluster(pnum)
local orig_coeff = -0.0879835

simulate bs_est_bc=r(est_bc), rep(1000): nickell_inf

sort bs_est_bc
list if _n==975

sum bs_est_bc, d
gen dev2 = (bs_est_bc -`orig_coeff')^2 * 1000/999

sum dev2
di ""
di "bias corrected estimate is `orig_coeff'" 
di "standard error is " sqrt(r(mean))


* Phase RDD3
use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear

destring playercorrect, replace
destring playerdiscontinuity_yn, replace

destring gorder,replace

keep if playertreatment_group == "A"

egen pnum = group(pid)

xtset pnum gorder

xtreg y L.y, fe cluster(pnum)
local orig_coeff = -0.00855235

simulate bs_est_bc=r(est_bc), rep(1000): nickell_inf

sort bs_est_bc
list if _n==975

sum bs_est_bc, d
gen dev2 = (bs_est_bc -`orig_coeff')^2 * 1000/999

sum dev2
di ""
di "bias corrected estimate is `orig_coeff'" 
di "standard error is " sqrt(r(mean))


* Phase RDD4
use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear

destring playercorrect, replace
destring playerdiscontinuity_yn, replace

destring gorder,replace

keep if playertreatment_group == "A"

egen pnum = group(pid)

xtset pnum gorder

xtreg y L.y, fe cluster(pnum)
local orig_coeff = -0.0391887

simulate bs_est_bc=r(est_bc), rep(1000): nickell_inf

sort bs_est_bc
list if _n==975

sum bs_est_bc, d
gen dev2 = (bs_est_bc -`orig_coeff')^2 * 1000/999

sum dev2
di ""
di "bias corrected estimate is `orig_coeff'" 
di "standard error is " sqrt(r(mean))

* Experts (small bins)
use "${dat}/expert_merged_rd_estimates_micro.dta", clear

destring playercorrect, replace
destring playerdiscontinuity_yn, replace

destring gorder,replace

keep if playertreatment_group == "A"

egen pnum = group(pid)

xtset pnum gorder

xtreg y L.y, fe cluster(pnum)
local orig_coeff = -0.125996

simulate bs_est_bc=r(est_bc), rep(1000): nickell_inf

sort bs_est_bc
list if _n==975

sum bs_est_bc, d
gen dev2 = (bs_est_bc -`orig_coeff')^2 * 1000/999

sum dev2
di ""
di "bias corrected estimate is `orig_coeff'" 
di "standard error is " sqrt(r(mean))

log close

								